﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Numeric;
using System.Data;
using System.Data.SqlClient;
using SQLExecute;
using DevExpress;


public partial class list_nghiphep : System.Web.UI.Page
{
    User u = new User();
    DataSet ds;
    NghiPhep n = new NghiPhep();
    int id;
    public string strMessage;
    string strFrom = "1/1/1900", strTo = "1/1/1900";
    Common c = new Common();
    DateTime dt1, dt2;
    CultureInfo culture;
    DateTimeStyles styles = DateTimeStyles.None;


    protected void Page_Init(object sender, EventArgs e)
    {
        ShowInfo();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack&&!IsCallback)
            if (Session["email"] != null && Session["fullname"] != null)
            {

                
                ShowInfo();
                lUsername.Text = Session["username"].ToString();
            }
           
           else { Response.Redirect("../login.aspx"); }        
        n.List_Nghi_Phep_Manager(gdNghiPhep);

        
    }
    protected void ShowInfo()
    {        
        //c.InitCombo(cbPhongBan, 3, 0);       
        n.List_Nghi_Phep_Manager(gdNghiPhep);
    }
    

    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("../login.aspx");
    }
    protected void gdNghiPhep_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
    {
        int id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "id");
        string sql = string.Empty;
     
        if (e.ButtonID == "btnDelete" )
        {
            sql = "update tbl_nghiphep set active=0 where id='" + id + "'";
            SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);            
            n.List_Nghi_Phep_Manager(gdNghiPhep);            
        }
        
    }
    protected void cbPhongBan_SelectedIndexChanged(object sender, EventArgs e)
    { 
        if (txtFromDate.Text != "" && txtToDate.Text != ""&&cbPhongBan.SelectedIndex>0)
            {
                strFrom = Common.Convert_DDMMYYYY_To_MMDDYYY(txtFromDate.Text);
                strTo = Common.Convert_DDMMYYYY_To_MMDDYYY(txtToDate.Text);
                n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep, DateTime.Parse(strFrom), DateTime.Parse(strTo));
            }
        
         else
              n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep,null,null);
    }
    protected void gdNghiPhep_RowCommand1(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        int nValue;
        string sql = string.Empty;
        DateTime dt;
        string s1,s2;
        if (e.CommandArgs.CommandName == "DuyetNghiPhep")
        {
            id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghiphepid");
            string strValue=(string)gdNghiPhep.GetRowValues(e.VisibleIndex,"giamdoc");
            dt = (DateTime)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghipheptungay");

            if (dt > DateTime.Now||(Math.Abs(dt.DayOfYear-DateTime.Now.DayOfYear)<=7))
            {
                if (strValue == "Chờ")
                    nValue = 1;
                else nValue = 0;
                n.Duyet_Nghi_Phep(id, nValue,3);
                n.List_Nghi_Phep_Manager(gdNghiPhep);           
            }
        }
        if (e.CommandArgs.CommandName == "XoaNghiPhep")
        {
            id = (int)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghiphepid");
            dt = (DateTime)gdNghiPhep.GetRowValues(e.VisibleIndex, "nghipheptungay");


            if (dt > DateTime.Now || (Math.Abs(dt.DayOfYear - DateTime.Now.DayOfYear) <= 7))
            {
               
                n.Xoa_Nghi_Phep(id);
                n.List_Nghi_Phep_Manager(gdNghiPhep);               
            }
        }

    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        /*
        if (txtFromDate.Text!="" && txtToDate.Text != "")
        {

            culture = CultureInfo.CreateSpecificCulture("fr-FR");
            DateTime.TryParse(txtFromDate.Text, culture, styles, out dt1);
            DateTime.TryParse(txtToDate.Text, culture, styles, out dt2);
            n.List_Nghi_Phep_Theo_PhongBan(cbPhongBan, gdNghiPhep,dt1,dt2);
        }
         */
    }

    protected void btnReload_Click(object sender, EventArgs e)
    {
        n.List_Phep_Theo_PhongBan_All(gdNghiPhep);
    }



    protected void gdNghiPhep_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#f7f7f7';");
        if ((e.VisibleIndex % 2) == 0)
        {
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
        }
        else
        {
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
        }
    
    }
}
